<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2020/3/3
 * Time: 15:56
 * 用户信息缓存组件
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Cache\Cache;

class VideoCacheComponent extends Component
{

    public $_cache_config = 'videos';
    public $_page_num = 30;
    public $components = ['CategoryContentCache'];
    public function initialize(array $config)
    {
        parent::initialize($config);
    }

    /**
     * 具体信息
     */
    public function getVal($id)
    {
        $key = 'video_' . $id;
        $data = Cache::read($key, $this->_cache_config);
        return empty($data) ? $this->createCache($id) : $data;
    }

    /**
     * 创建缓存
     */
    public function createCache($video_id)
    {
        $key = 'video_' . $video_id;
        $controller = $this->_registry->getController();
        $controller->loadModel('Videos');
        $data = $controller->Videos->find()
            ->where(['id' => $video_id])
            ->first();
        $cache = empty($data) ? null : $data;
        Cache::write($key, $cache, $this->_cache_config);
        return $cache;
    }

    /**
     * 删除列表
     */
    public function deleteList()
    {
        Cache::delete('list', $this->_cache_config);
        return true;
    }

    /**
     * 删除缓存
     */
    public function deleteCache($id)
    {
        $key = 'video_' . $id;
        Cache::delete($key, $this->_cache_config);
        return true;
    }

    /**
     * 获取列表信息
     */
    public function getList($data)
    {
        $key = md5(json_encode($data));
        $controller = $this->_registry->getController();
        $controller->loadModel('Videos');
        $controller->loadModel('CategoryContents');
        $cache = Cache::read('list', $this->_cache_config);
        if(empty($cache[$key])){
            $category_where = [];
            $where = [];
            $category_where['content_type'] = 'videos';
            if(!empty($data['category_id'])){
                $category_where['category_id'] = $data['category_id'];
                $category_content = $controller->CategoryContents->find()
                    ->select(['content_id'])
                    ->where($category_where)
                    ->order(['order_num desc'])
                    ->toArray();
                if(!empty($category_content)){
                    $where['id in'] = array_column($category_content, 'content_id');
                }
            }
            if(!empty($data['year'])){
                $where['year'] = $data['year'];
            }
            if(!empty($data['status'])){
                $where['status in'] = $data['status'];
            }
            if(isset($data['enabled']) && in_array($data['enabled'], [0,1]) && is_numeric($data['enabled'])){
                $where['enabled'] = $data['enabled'];
            }
            $page = empty($data['page']) ? 1 :$data['page'];
            if(!empty($data['title'])){
                $where['title like'] = '%'.$data['title'].'%';
            }
            $thread = $controller->Videos->find()
                ->select(['id'])
                ->where($where)
                ->order(['id desc'])
                ->limit($this->_page_num)
                ->page($page);
            $cache[$key]['total'] = $thread->count();
            $thread = $thread->toArray();
            $cache[$key]['list'] = empty($thread) ? [] : array_column($thread, 'id');
            Cache::write('list', $cache, $this->_cache_config);
        }
        return $cache;
    }

}